package com.bt;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import com.btnhom.IntegerTextField;

public class F_Calculator extends JFrame implements ActionListener {

	private JPanel contentPane;
	private IntegerTextField txtNumber;
	private boolean[] function = new boolean[4];
	private double[] temporary = { 0, 0 };
	private StringBuilder str;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					F_Calculator frame = new F_Calculator();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public F_Calculator() {
		str = new StringBuilder();
		setTitle("Calculator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 360, 325);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JButton btt_0 = new JButton("0");
		btt_0.addActionListener(this);
		btt_0.setBounds(10, 244, 121, 31);
		contentPane.add(btt_0);

		JButton btt_1 = new JButton("1");
		btt_1.addActionListener(this);
		btt_1.setBounds(10, 201, 54, 31);
		contentPane.add(btt_1);

		JButton btt_2 = new JButton("2");
		btt_2.addActionListener(this);
		btt_2.setBounds(74, 201, 54, 31);
		contentPane.add(btt_2);

		JButton btt_3 = new JButton("3");
		btt_3.addActionListener(this);
		btt_3.setBounds(141, 201, 54, 31);
		contentPane.add(btt_3);

		JButton btt_4 = new JButton("4");
		btt_4.addActionListener(this);
		btt_4.setBounds(10, 159, 54, 31);
		contentPane.add(btt_4);

		JButton btt_5 = new JButton("5");
		btt_5.addActionListener(this);
		btt_5.setBounds(74, 159, 54, 31);
		contentPane.add(btt_5);

		JButton btt_6 = new JButton("6");
		btt_6.addActionListener(this);
		btt_6.setBounds(141, 159, 54, 31);
		contentPane.add(btt_6);

		JButton btt_7 = new JButton("7");
		btt_7.addActionListener(this);
		btt_7.setBounds(10, 119, 54, 31);
		contentPane.add(btt_7);

		JButton btt_8 = new JButton("8");
		btt_8.addActionListener(this);
		btt_8.setBounds(74, 119, 54, 31);
		contentPane.add(btt_8);

		JButton btt_9 = new JButton("9");
		btt_9.addActionListener(this);
		btt_9.setBounds(141, 119, 54, 31);
		contentPane.add(btt_9);

		JButton btt_OFF = new JButton("OFF");
		btt_OFF.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		}); 
		btt_OFF.addActionListener(this);
		btt_OFF.setBounds(10, 77, 115, 31);
		contentPane.add(btt_OFF);

		JButton btt_1x = new JButton("1/x");
		btt_1x.setBounds(279, 159, 54, 31);
		contentPane.add(btt_1x);

		JButton btt_dec = new JButton(".");
		btt_dec.addActionListener(this);
		btt_dec.setBounds(141, 244, 54, 31);
		contentPane.add(btt_dec);

		JButton bttNP = new JButton("+/-");
		bttNP.addActionListener(this);
		bttNP.setBounds(211, 77, 54, 31);
		contentPane.add(bttNP);

		JButton btt_D = new JButton("/");
		btt_D.addActionListener(this);
		btt_D.setBounds(211, 119, 54, 31);
		contentPane.add(btt_D);

		JButton btt_P = new JButton("+");
		btt_P.addActionListener(this);
		btt_P.setBounds(211, 244, 54, 31);
		contentPane.add(btt_P);

		JButton btt_S = new JButton("-");
		btt_S.addActionListener(this);
		btt_S.setBounds(211, 201, 54, 31);
		contentPane.add(btt_S);

		JButton btt_M = new JButton("*");
		btt_M.addActionListener(this);
		btt_M.setBounds(211, 159, 54, 31);
		contentPane.add(btt_M);

		JButton btt_Sqrt = new JButton("\u221A");
		btt_Sqrt.addActionListener(this);
		btt_Sqrt.setBounds(279, 77, 54, 31);
		contentPane.add(btt_Sqrt);

		JButton btt_Percent = new JButton("%");
		btt_Percent.addActionListener(this);
		btt_Percent.setBounds(279, 119, 54, 31);
		contentPane.add(btt_Percent);

		JButton btt_E = new JButton("=");
		btt_E.addActionListener(this);
		btt_E.setBounds(279, 201, 54, 74);
		contentPane.add(btt_E);

		txtNumber = new IntegerTextField();
		txtNumber.setText("0");
		txtNumber.setHorizontalAlignment(SwingConstants.RIGHT);
		txtNumber.setFont(new Font("Tahoma", Font.BOLD, 14));
		txtNumber.setBounds(10, 11, 324, 55);
		contentPane.add(txtNumber);

		JButton button = new JButton("C");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					txtNumber.setText("0"); // blank
					str = new StringBuilder();
					for (int i = 0; i < 4; i++)
						function[i] = false; // functions = false
					for (int i = 0; i < 2; i++)
						temporary[i] = 0; // temporary = 0
				} catch (NullPointerException e) {
				}
			}
		});
		button.setBounds(141, 77, 54, 31);
		contentPane.add(button);
	}

	private Object CheckInteger(double number) {
		boolean isInt = number == (int) number;
		if (isInt)
			return (int) number;
		else
			return number;
	}
	
	private void Math_Sqrt() {
		try {
			double value = Double.parseDouble(txtNumber.getText());
			value = Math.sqrt(value);
			txtNumber.setText(String.valueOf(CheckInteger(value)));
		} catch (NumberFormatException e) {

		}
	}

	private void ChangeNP() {
		try {
			double value = Double.parseDouble(txtNumber.getText());
			if (value != 0) {
				value = value * (-1);
				txtNumber.setText(String.valueOf(CheckInteger(value)));
			}

		} catch (NumberFormatException e) {
		}
	}

	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton s = (JButton) e.getSource();
		switch (s.getText()) 
		{
		case "0":
			if (str.toString().indexOf("0") != 0)
			{
				str.append("0");
				txtNumber.setText(str.toString());
			}
			break;
		case ".":
			if (str.toString().indexOf(".") == -1)
			{ // not exist decimal
				str.append(".");
				if (str.toString().indexOf(".") == 0)
					str.insert(0, "0");
				txtNumber.setText(str.toString());
			}
			break;
		case "1":
			// if(str.toString().indexOf("0"))
			str.append("1");
			txtNumber.setText(str.toString());
			break;
		case "2":
			// if(str.toString().indexOf("0"))
			str.append("2");
			txtNumber.setText(str.toString());
			break;
		case "3":
			// if(str.toString().indexOf("0"))
			str.append("3");
			txtNumber.setText(str.toString());
			break;
		case "4":
			// if(str.toString().indexOf("0"))
			str.append("4");
			txtNumber.setText(str.toString());
			break;
		case "5":
			// if(str.toString().indexOf("0"))
			str.append("5");
			txtNumber.setText(str.toString());
			break;
		case "6":
			// if(str.toString().indexOf("0"))
			str.append("6");
			txtNumber.setText(str.toString());
			break;
		case "7":
			// if(str.toString().indexOf("0"))
			str.append("7");
			txtNumber.setText(str.toString());
			break;
		case "8":
			// if(str.toString().indexOf("0"))
			str.append("8");
			txtNumber.setText(str.toString());
			break;
		case "9":
			// if(str.toString().indexOf("0"))
			str.append("9");
			txtNumber.setText(str.toString());
			break;
		case "+/-":
			// if(str.toString().indexOf("0"))
			str.append("-");
			txtNumber.setText(str.toString());
			break;
		case "/":
			// if(str.toString().indexOf("0"))
			str.append("/");
			txtNumber.setText(str.toString());
			break;
		case "*":
			// if(str.toString().indexOf("0"))
			str.append("*");
			txtNumber.setText(str.toString());
			break;
		case "+":
			// if(str.toString().indexOf("0"))
			str.append("+");
			txtNumber.setText(str.toString());
			break;
		case "-":
			// if(str.toString().indexOf("0"))
			str.append("-");
			txtNumber.setText(str.toString());
			break;
		case "%":
			// if(str.toString().indexOf("0"))
			str.append("%");
			txtNumber.setText(str.toString());
			break;
		case "\u221A":
			// if(str.toString().indexOf("0"))
			str.append("\u221A");
			txtNumber.setText(str.toString());
			break;	
		case "=":
			// if(str.toString().indexOf("0"))
			str.append("=");
			txtNumber.setText(str.toString());
			break;
		}

	}
}
